{\rtf1\ansi\ansicpg1252\cocoartf1187\cocoasubrtf390
{\fonttbl\f0\fswiss\fcharset0 Helvetica;}
{\colortbl;\red255\green255\blue255;}
\margl1440\margr1440\vieww17040\viewh12780\viewkind0
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural

\f0\fs36 \cf0 Implementing a stack using single linked list as underlying stub\

\fs24 \
typedef struct Node \{\
    Node *next;\
    int data;\
    Node(int d): data(d) \{next = NULL;\}\
\};\
\
class Stack \{\
public:\
    Stack() \{top = NULL\};\
    ~Stack() \{\
        while (top) \{\
            Node *cur = top;\
            top = top->next;\
            delete cur;\
        \}\
    \}\
\
    void push(int data);\
    bool top(int &data);\
    bool pop(int &data);\
\
private:\
    Node *top;\
\};\
\
void Stack::push(int data) \{\
    Node *cur = top;\
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\cf0     top = new Node(data);\
    top->next = cur;    \
\pard\tx720\tx1440\tx2160\tx2880\tx3600\tx4320\tx5040\tx5760\tx6480\tx7200\tx7920\tx8640\pardirnatural
\cf0 \}\
\
bool top(int &data) \{\
    if (!top) return false;\
    data = top->data;\
    return true;\
\}\
\
bool pop(int &data) \{\
    if (!top) return false;\
    Node *cur = top; \
    top = top->next;\
    data = cur->data;    // de-allocate node\
    delete cur;\
    return true;\
\}\
}